<?php
/*
Plugin Name: Extra User Details
Plugin URI: http://vadimk.com/wordpress-plugins/extra-user-details/
Description: Allows you to add additional fields to the user profile like Facebook, Twitter etc.
Author: Vadim Khukhryansky
Version: 0.2
Author URI: http://vadimk.com/
*/

if ( ! get_option( 'eud_fields' ) ) add_option( 'eud_fields', '' );

add_action( 'edit_user_profile', 'eud_extract_ExtraFields' );
add_action( 'show_user_profile', 'eud_extract_ExtraFields' );
add_action( 'profile_update', 'eud_update_ExtraFields' );

//Administration
add_action( 'admin_menu', 'eud_plugin_menu' );
add_action('init', 'eud_scripts');

function eud_plugin_menu() {
	add_submenu_page( 'users.php', 'Extra User Details Options', 'Extra User Details', 8, 'extra_user_details', 'eud_plugin_options' );
}

function eud_scripts(){
	wp_enqueue_script( array("jquery", "jquery-ui-core", "jquery-ui-sortable") ); 
}


function eud_extract_ExtraFields() {
	if ( get_option( 'eud_fields' ) ) {
		
		$all_fields = unserialize( get_option( 'eud_fields' ) );

		if ( count( $all_fields ) > 0 ) {
	
			echo '<h3>' . __( 'Extra User Details', 'extra_user_information' ) . '</h3>
					<table class="form-table">';
	
			foreach ( $all_fields as $key => $value ) {
			
			echo '<tr>
					<th><label for="eud' . esc_attr( $value[1] ) . '">' . esc_attr( $value[0] ) . '</label></th>
						<td><input name="eud' . esc_attr( $value[1] ) . '" id="eud' . esc_attr( $value[1] ) . '" type="text" value="' . esc_attr( get_user_meta( get_user_id(), $value[1], true ) ) . '" class="regular-text code" />&nbsp;<span class="description">' . ( ( isset( $value[2] ) && $value[2] !== '' ) ? esc_attr( stripslashes( $value[2] ) ) : '' ) . '</span></td>
				</tr>';
			}
			
			echo '</table>';
		}
	}
}




function eud_plugin_options() {
	
	// ============
	// = Updating =
	// ============
	if ( isset( $_POST['eud_submit'] ) && isset( $_POST['eud_fields'] ) ) {

		$eud_fields = $_POST['eud_fields'];
		$all_fields = array();
		$error_fields = array();

		foreach ( $eud_fields as $key => $value ) {

			if ( isset( $value['0'] ) || isset( $value['1'] ) ) {
				
				$name = !empty( $value['0'] ) ? $value['0'] : ucfirst( $value['1'] );
				$slug = !empty( $value['1'] ) ? $value['1'] : sanitize_title( $value['0'] );
				$description = !empty( $value['2'] ) ? $value['2'] : '';
				
				$eud_fields[$key][0] = $name;
				$eud_fields[$key][1] = $slug;
				$eud_fields[$key][2] = $description;
				
				
				// Validate the field label
				if ( preg_match( '/^[0-9a-z-_]+$/', $slug ) === 1 ) {
					$all_fields[] = array(
										0 => $name,
										1 => $slug,
										2 => $description,
										);
				} else {
					$error_fields[] = $key;
				}
			}
			
		}

		if( count( $all_fields ) > 0) {
			update_option( 'eud_fields', serialize( $all_fields ) );
		} else {
			update_option( 'eud_fields', '' );
		}

		echo '<div class="updated"><p><strong>' . __( 'Extra Fields Options Updated.', 'extra_user_details' ) . '</strong></p></div>';
		
		if ( count( $error_fields ) )
			echo '<div class="error">
					<p><strong>' . __( 'However, highlighted fields are not updated because contain errors! Please correct them and update fields again or this fields will be lost.', 'extra_user_details' ) . '</strong></p>
				</div>';
		
	}
	
	
	echo '
	<form action="" method="post">
		<div class="wrap">
			<div class="icon32" id="icon-options-general"></div>
			<h2>' . __( 'Extra User Details Options', 'extra_user_details' ) . '</h2>';

		echo '
			<form action="" method="post">

				<h3>' . __( 'Currently defined fields', 'extra_user_details' ) . '</h3>';

				if ( isset( $eud_fields ) && !empty( $eud_fields ) ) {
					$all_fields = $eud_fields;
				}

				if ( !isset( $all_fields ) || empty( $all_fields ) ) {
					$all_fields = get_option( 'eud_fields' );
					if ( !is_array( $all_fields) ) {
						$all_fields = unserialize( $all_fields );
					}
				}

?>
					<script type='text/javascript'> 
						jQuery(document).ready(function(){ 

							jQuery('.sortcontainer').sortable({
								axis: 'y',
								items: 'div',
								stop: function() {
									jQuery(this).find('div').each(function(index) {
										jQuery(this).find('td:eq(0)').html((index + 1) + '.');
									})
								}
							 });
							
							jQuery('.add-field').click(function(){
								var randnum = Math.floor((Math.random() * (9999-999+1))+999);
								var append = '<div class="field"><table><tbody><tr><th class="num">&nbsp;</th><th class="slug"><?php echo __( "Meta Key:", "extra_user_details" ); ?></th><th class="name"><?php echo __( "Field Name:", "extra_user_details" ); ?></th><th class="description"><?php echo __( "Description (Help Text):", "extra_user_details" ); ?></th><th class="actions"></th></tr><tr><td>' + (jQuery('.sortcontainer div').size() + 1) + '.</td><td><input name="eud_fields['+ randnum +'][1]" type="text" value="" class="regular-text" size="" /></td><td><input name="eud_fields['+ randnum +'][0]" type="text" value="" class="regular-text" size="" /></td><td><input name="eud_fields['+ randnum +'][2]" type="text" value="" class="regular-text" size="80" /></td><td> <input type="button" value="Delete" class="delete-field button"></td></tr></tbody></table></div>';
								jQuery('.sortcontainer').append(append);
							});
							
							jQuery('.delete-field').live('click', function() {
								var parent = jQuery(this).parents('.field');
								if (parent.find('input[type=text]').val() == '' || confirm('Are you sure you want to delete this extra field?')) {
									parent.remove();
								}
								return false;
							});
							
						});
					</script>
					
					<style type='text/css'>
						.sortcontainer{
							padding:0 0 5px 0;
						}
						.sortcontainer .field{
							margin:8px 0;
							cursor:ns-resize;
						}
						.sortcontainer .field table{
							background-color:#f1f1f1;
							border:1px dotted gray;
							padding:5px;
							-moz-border-radius:5px 5px 5px 5px;
							border-radius:5px 5px 5px 5px;
						}
						.sortcontainer .field table.bad-field{
							background-color:#FFEBE8;
							border-color:#CC0000;
						}
						.sortcontainer .field input.regular-text{
							width:100%;
						}
						.sortcontainer .field th{
							font-size:10px;
							font-style:normal;
						}
						.sortcontainer .field td{
							padding:0 5px;
						}
						.sortcontainer .num{
							width:3%;
						}
						.sortcontainer .slug{
							width:15%;
						}
						.sortcontainer .name{
							width:25%;
						}
						.sortcontainer .description{
							width:50%;
						}
						.sortcontainer .actions{
							width:7%;
						}
						.sortcontainer .delete{
							font-size:11px;
							font-style:italic;
							color:#666;
							float:right;
						}
					</style>
					
				<div class="sortcontainer">
<?php

				if ( is_array( $all_fields ) && count( $all_fields ) > 0 ) {
					
					if ( !isset( $error_fields ) ) $error_fields = array();

					$counter = 0;
					foreach ( $all_fields as $key => $value ) {
						if ( in_array( $key, $error_fields) ) $class = ' class="bad-field"'; else $class = '';
						echo '
						<div class="field">
							<table'.$class.'>
								<tbody>
									<tr>
										<th class="num">&nbsp;</th>
										<th class="slug">' . __( 'Meta Key:', 'extra_user_details' ) . '</th> 
										<th class="name">' . __( "Field Name:", "extra_user_details" ) . '</th>
										<th class="description">' . __( "Description (Help Text):", "extra_user_details" ) . '</th>
										<th class="actions"></th>
									</tr>
									<tr>
										<td>'.++$counter.'.</td>
										<td>
											<input name="eud_fields[' . esc_attr( $key ) . '][1]" type="text" value="' . esc_attr( $value[1] ) . '" class="regular-text" size="20" />
										</td>
										<td>
											<input name="eud_fields[' . esc_attr( $key ) . '][0]" type="text" value="' . esc_attr( $value[0] ) . '" class="regular-text" size="30" />
										</td>
										<td>
											<input name="eud_fields[' . esc_attr( $key ) . '][2]" type="text" value="' . ( isset( $value[2] ) ? htmlspecialchars( stripslashes( $value[2] ), ENT_NOQUOTES ) : '' ) . '" class="regular-text" size="80" />
										</td>
										<td>
											<input type="button" value="Delete" onclick="remove_eud_field();" class="delete-field button">
										</td>
									</tr>
								</tbody>
							</table>
						</div>';

					}
					unset( $value );
				}
					echo '</div>';
					
				unset( $all_fields );
				

				echo '
				</div>
				
				<input type="button" value="Add New Field" class="add-field button">
				
			<p class="submit">
			<input type="submit" name="eud_submit" class="button-primary" value="' . __( 'Update Extra Fields', 'extra_user_information' ) . '">
		</p>
	</form>';

	eud_help();
}


/**
 * Prints help info
 *
 * @return void
 * @author Vadimk
 **/
function eud_help() {
	echo '
	<div id="eud-fields-help">
		<p><strong>' . __( "How to use this plugin:", 'extra_user_details' ) . '</strong></p>
		<p>' . __( "<strong>Meta key</strong>: This is the key to use in templates with <em>get_user_meta('meta_key')</em>. No special characters please, only lowercase letters, '_' and '-' can be used. <strong>NOTE:</strong> If you change this slug later, this will NOT update meta key for existing meta values in DB!", 'extra_user_details'  ) . '</p>
		<p>' . __( "<strong>Field label</strong>: This is the label your users will see on their profile.", 'extra_user_details' ) . '</p>
		<p>' . __( "<strong>Help text</strong>: Description text is showing near the appropriate custom field in user profile.", 'extra_user_details' ) . '<br />' . __( 'The default help text is:', 'extra_user_details') . ' <em>' . __( 'Please fill in this additional profile information field.', 'extra_user_information' ) . '</em></p>
		<p>' . __( "<strong>Field order</strong>: Fields are normally displayed - both here as well as on user profiles - in the order in which they are added. If you would like to change the order in which these extra fields are displayed: simply move the field above or below another one and update settings.", 'extra_user_details' ) . '</p>
		<p>' . __( "<strong>WARNING</strong>: Please remember, you're using this plugin for your own risk and should know what you're doing, because wp_usermeta table contains other meta_keys for another purpose which can be affected (if you set the same slug for your extra field). If you will add two identical fields - the user won't be able to update those fields in profile.", 'extra_user_details' ) . '</p>
	<div>';
}

/**
 * Updates extra fields. Input validation needed, but we don't know what the user wants to accept...
 *
 * @return void
 * @author Vadimk
 */
function eud_update_ExtraFields() {
	$get_user_id = get_user_id();

	foreach ( $_POST as $key => $value ) {

		if( eregi( 'eud', $key ) ) {

			$key = str_replace( 'eud', '', $key );

			if( !empty( $value ) ) {
				eud_update_option( $get_user_id, $key, $value );
			}
			else {
				eud_delete_option( $get_user_id, $key, $value );
			}
		}
	}
}

/**
 * Updates extra field value
 *
 * @param string $get_user_id 
 * @param string $eudfield 
 * @param string $value 
 * @return void
 * @author Vadimk
 */
function eud_update_option( $get_user_id, $eudfield, $value ) {
	if( function_exists( 'update_user_meta' ) ) {
		update_user_meta( $get_user_id, str_replace( 'eud', '', $eudfield ), $value );
	}
	else {
		update_usermeta( $get_user_id, str_replace( 'eud', '', $eudfield ), $value );
	}
}

/**
 * Deletes extra field value if nothing was populated
 *
 * @param string $get_user_id 
 * @param string $eudfield 
 * @param string $value 
 * @return void
 * @author Vadimk
 */
function eud_delete_option( $get_user_id, $eudfield, $value){
	if( function_exists( 'delete_user_meta' ) ) {
		delete_user_meta( $get_user_id, str_replace( 'eud', '', $eudfield ), $value );
	}
	else {
		delete_usermeta( $get_user_id, str_replace( 'eud', '', $eudfield ), $value );
	}
}

/**
 * Return current editing user_id
 *
 * @return int
 * @author Vadimk
 */
function get_user_id() {
	
	$get_user_id = empty( $_GET['user_id'] ) ? null : $_GET['user_id'];
	
	if ( ! isset( $get_user_id ) ) {
		$get_user_id = empty( $_POST['user_id'] ) ? null : $_POST['user_id'];
	}
	
	if ( ! isset( $get_user_id ) ) {
		global $current_user;
		get_currentuserinfo();
		$get_user_id = $current_user->ID;
	}

	return $get_user_id;
}


/* Wrapper for esc_attr for backward compatibility */
if ( ! function_exists( 'esc_attr' ) ) {
	function esc_attr( $text ) {
		return attribute_escape( $text );
	}
}

/* Wrapper for esc_html for backward compatibility */
if ( ! function_exists( 'esc_html' ) ) {
	function esc_html( $text ) {
		return wp_specialchars( $text );
	}
}

/* Wrapper for get_user_meta for backward compatibility */
if ( ! function_exists( 'get_user_meta' ) ) {
	function get_user_meta( $userid, $metakey, $single ) {
		return get_usermeta( $userid, $metakey );
	}
}